import { Injectable } from '@angular/core';
import { BaseService } from '@core/routes-services/base.service';
import { CacheService } from '@core/routes-services/cache.service';
import { HttpClient } from '@angular/common/http';
import { myValidNull } from '@core/util/string-utils';

/**
 * otc 运营中心 服务
 */
@Injectable()
export class CenterService extends BaseService {

  constructor(http: HttpClient, cacheService: CacheService) {
    super(http, cacheService);
  }

  base_url: any = 'api/otcOperationCenter';


  /**
   * 分页
   *
   * @param user
   * @returns {Promise<any>}
   */
  pagination(page: any, keyword: any): Promise<any> {
    const url = this.base_url + '/pagination';
    if(keyword){
      page['_filter_like_title'] = keyword;
    }
    page['_sort_by_'] = "credit_asc";
    return this.post(url, page);
  }

  /**
   * 分页
   *
   * @param user
   * @returns {Promise<any>}
   */
  salesmanPagination(page: any, keyword: any,centerId:any, sortName: any, sortValue: any): Promise<any> {
    const url = this.base_url + '/actions/salesman/pagination';
    if(keyword){
      page['_filter_like_name'] = keyword;
    }
    if(centerId){
      page['_filter_eq_centerId'] = centerId;
    }
    if (myValidNull(sortName) || myValidNull(sortValue)) {
      page['_sort_by_'] = "shopNum_desc";
    } else {
      page['_sort_by_'] = sortName + '_' + (sortValue == 'ascend' ? 'asc' : 'desc');
    }
    return this.post(url, page);
  }

  /**
   * 获取积分明细分页
   *
   * @param user
   * @returns {Promise<any>}
   */
  creditPagination(page: any, keyword: any,operateType: any,customerId:any, centerId:any, filterCenterNull:any): Promise<any> {
    const url = this.base_url + '/credit/pagination';
    if (keyword) {
      page['_filter_like_orderNo'] = keyword;
    }
    if (filterCenterNull) {
      page[filterCenterNull] = '1';
    }
    if (operateType) {
      page['_filter_eq_operateType'] = operateType;
    }
    if (centerId) {
      page['_filter_eq_centerId'] = centerId;
    }
    if (customerId) {
      page['_filter_eq_cusId'] = customerId;
      page['_filter_isnull_centerId'] = customerId;
    }
    page['_sort_by_'] = "doTime_desc";
    return this.post(url, page);
  }
  /**
   * 修改
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  saveOrUpdateEntity(id: any, entity: any): Promise<any> {
    if (id) {
      const url = this.base_url + '/' + id;
      return this.put(url, entity);
    } else {
      const url = this.base_url;
      return this.post(url, entity);
    }
  }

  /**
   * 获取查看详情
   *
   * @param id
   * @returns {Promise<any>}
   */
  detail(id: any): Promise<any> {
    const url = this.base_url + '/' + id;
    return this.get(url);
  }

  /**
   * 删除
   *
   * @param ids
   * @returns {Promise<any>}
   */
  deleteEntity(ids): Promise<any> {
    const url = this.base_url;
    return this.delete(url, ids);
  }
  /**
   * 获取全部
   *
   * @returns {Promise<any>}
   */
  getAll(): Promise<any> {
    const url = this.base_url + '/all';
    return this.get(url);
  }
}
